package org.redpig.mimall;

import lombok.extern.slf4j.Slf4j;
import org.redpig.mimall.consts.MimallConsts;
import org.redpig.mimall.exception.UserLoginException;
import org.redpig.mimall.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class UserLoginInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("preHandle...start");
        User user = (User) request.getSession().getAttribute(MimallConsts.CURRENT_USER);
        if (null == user) {
            log.info("user=null");
            throw new UserLoginException();
        }
        log.info("preHandle...end");
        return true;
    }
}
